﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.Composite.Extensions;
using Microsoft.Practices.Composite.Regions;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.Help.Interface;
using Jbe.TestSuite.LogViewer.Demo.Properties;
using System.Collections.Specialized;
using System.Reflection;
using Jbe.TestSuite.Infrastructure.Interface.Documents;

namespace Jbe.TestSuite.LogViewer.Demo.Applications
{
    internal class ModuleController
    {
        private readonly IApplicationContainer container;
        private readonly IRegionManager regionManager;
        private readonly IUIElementCreationService uiElementCreationService;
        private IDynamicHelp dynamicHelp;
        private DelegateCommand closeCommand;
        private LogDemoPresentationModel presentationModel;


        public ModuleController(IApplicationContainer container, IRegionManager regionManager, IUIElementCreationService uiElementCreationService)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.uiElementCreationService = uiElementCreationService;
        }


        public void Initialize()
        {
            dynamicHelp = container.TryResolve<IDynamicHelp>();

            closeCommand = new DelegateCommand(Close);
            DocumentCommands.Close.RegisterCommand(closeCommand);

            IToolBarItem toolBarItem = uiElementCreationService.CreateToolBarItem(Resources.LogDemo, Resources.ReportEdit.ToBitmap());
            toolBarItem.Click += ToolBarItemClick;
            regionManager.Regions[ShellRegionNames.MainToolBar].Add(toolBarItem);
        }

        private void ToolBarItemClick(object sender, EventArgs e)
        {
            if (presentationModel == null)
            {
                presentationModel = container.Resolve<LogDemoPresentationModel>();
                regionManager.Regions[ShellRegionNames.ContentRegion].ActiveViews.CollectionChanged += ActiveViewsCollectionChanged;
                regionManager.Regions[ShellRegionNames.ContentRegion].Add(presentationModel.View);
            }
            regionManager.Regions[ShellRegionNames.ContentRegion].Activate(presentationModel.View);
        }


        private void ActiveViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Contains(presentationModel.View))
            {
                closeCommand.IsActive = true;
                if (dynamicHelp != null)
                {
                    dynamicHelp.ShowHelp(Assembly.GetExecutingAssembly(), "Resources/Help/DemoPage.htm");
                }
            }
            else if (e.OldItems != null && e.OldItems.Contains(presentationModel.View)) { closeCommand.IsActive = false; }
        }

        private void Close()
        {
            regionManager.Regions[ShellRegionNames.ContentRegion].Remove(presentationModel.View);
            regionManager.Regions[ShellRegionNames.ContentRegion].ActiveViews.CollectionChanged -= ActiveViewsCollectionChanged;
            presentationModel = null;
        }
    }
}
